﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using CDB.BusinessLayer.Utilitarios;
using CDB.Model.Utilitarios;
using System.Collections.ObjectModel;
using System.Data.Common;
using Microsoft.Win32;


namespace ColaDaBase
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            w_GerarProc w = new w_GerarProc();            
            w.Show();
            this.Hide();

        }

        #region Eventos

        #region Botões
        private void btnNovo_Click(object sender, RoutedEventArgs e)
        {
            cvsNovo.Visibility = System.Windows.Visibility.Visible;
        }

        private void btnAtualizar_Click(object sender, RoutedEventArgs e)
        {
            this.GetServers();
            this.cbxServers.IsDropDownOpen = true;
        }

        private void btnEscolherBase_Click(object sender, RoutedEventArgs e)
        {
            this.OpenFileDialog();
        }

        private void btnSalvarConnection_Click(object sender, RoutedEventArgs e)
        {
            this.InsertXML();
        }

        private void btnTestConnection_Click(object sender, RoutedEventArgs e)
        {
            this.TestConnection();
        }
        #endregion

        #region Combobox
        private void cbxServers_TextChanged(object sender, TextChangedEventArgs e)
        {   
            
            if (cbxServers.Text == string.Empty)
            {
                gbxBanco.IsEnabled = false;
                txtCaminhoArquivo.Text = string.Empty;
                cbxDataBase.Text = string.Empty;
            }
            else
            {
                gbxBanco.IsEnabled = true;
                this.rdbSelecionarNomeBanco.IsChecked = true;
            }
            
        }

       
        private void cbxServers_GotFocus(object sender, RoutedEventArgs e)
        {
            
            if (cbxServers.Items.Count < 1)
            {
                this.GetServers();
            }

            cbxServers.IsDropDownOpen = true;
            
        }

        private void cbxDataBase_GotFocus(object sender, RoutedEventArgs e)
        {
            cbxDataBase.DataContext = this.GetDataBases();
            cbxDataBase.IsDropDownOpen = true;
        }

        private void cbxServers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (gbxBanco != null)
                gbxBanco.IsEnabled = true;
        }
        #endregion

        #region RadioButton
        private void rdbSelecionarNomeBanco_Checked(object sender, RoutedEventArgs e)
        {
            this.HabilitaSelecionarArquivo(false);   
        }
        private void rdbSelecionarArquivo_Checked(object sender, RoutedEventArgs e)
        {
            this.HabilitaSelecionarArquivo(true);
        }
        #endregion

        #endregion

        #region Métodos
        #region OpenFileDialog
        private void OpenFileDialog()
        {
            OpenFileDialog objFileDialog = new OpenFileDialog();
            objFileDialog.DefaultExt = ".dbo";
            objFileDialog.Filter = "SQL Server File (.dbo)|*.dbo";
            objFileDialog.ShowDialog();
            this.txtCaminhoArquivo.Text = objFileDialog.FileName; 
        }
        #endregion

        #region Insert
        private void InsertXML()
        {
            string strPath = AppDomain.CurrentDomain.BaseDirectory;
            strPath = @"C:\Documents and Settings\Renan\Meus documentos\XMLBD.xml";

            if((bool)rdbSelecionarArquivo.IsChecked)
                BLUtilitarios.InsertXMLDB(strPath, string.Empty, string.Empty, string.Empty, txtCaminhoArquivo.Text);
            else
                BLUtilitarios.InsertXMLDB(strPath, cbxDataBase.Text, txtUsuario.Text, txtPassword.Text, cbxServers.Text);
        }
        #endregion

        #region TestConnection
        private void TestConnection()
        {
            
            string strDataSouce = string.Empty;
            string strIntialCatalog = string.Empty;
            string strUsuario = this.txtUsuario.Text.Trim();
            string strSenha = this.txtPassword.Text.Trim();

            strDataSouce = this.cbxServers.Text.Trim();

            if (((bool)rdbSelecionarArquivo.IsChecked))
            {
                strIntialCatalog = this.txtCaminhoArquivo.Text.Trim();
            }
            else
            {

                strIntialCatalog = this.cbxDataBase.Text.Trim();
            }

            if (string.IsNullOrEmpty(strDataSouce))
            {
                System.Windows.MessageBox.Show("Falhou ao tentar conectar.", this.Title, MessageBoxButton.OK, MessageBoxImage.Stop);
                return;
            }

            if(string.IsNullOrEmpty(strIntialCatalog))
            {
                System.Windows.MessageBox.Show("Falhou ao tentar conectar.", this.Title,MessageBoxButton.OK,MessageBoxImage.Stop);
                return;
            }

            try
            {
                Mouse.OverrideCursor = Cursors.Wait;
                DbProviderFactory objDbProvider = DbProviderFactories.GetFactory("System.Data.SqlClient");
                DbConnection objDbConnection = objDbProvider.CreateConnection();

                objDbConnection.ConnectionString = BLUtilitarios.GetStringConexao(strDataSouce, strIntialCatalog, strUsuario, strSenha);

                objDbConnection.Open();

                if (objDbConnection.State == ConnectionState.Open)
                {
                    System.Windows.MessageBox.Show("Conectado com sucesso!", this.Title);
                    objDbConnection.Close();
                }
                else
                    System.Windows.MessageBox.Show("Falhou ao tentar conectar.", this.Title);

                objDbConnection.Dispose();

            }
            catch (DbException ex)
            {
                System.Windows.MessageBox.Show(ex.Message, this.Title);
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                Mouse.OverrideCursor = null;
            }
        }
        #endregion

        #region GetServers
        private void GetServers()
        {
            Mouse.OverrideCursor = Cursors.Wait;            
            List<ServerBD> lstServerDB = BLUtilitarios.GetServers();

            cbxServers.ItemsSource = lstServerDB;
            Mouse.OverrideCursor = null;
        }
        #endregion

        #region GetDataBases
        private Collection<string> GetDataBases()
        {
            Mouse.OverrideCursor = Cursors.Wait;
            string strBase = "master";
            string strUserName = txtUsuario.Text;
            string strPassword = txtPassword.Text;
            string strDataSource = cbxServers.Text;
            string strConnection = BLUtilitarios.GetStringConexao(strDataSource, strBase, strUserName, strPassword);
            BLUtilitarios objBLUtilitarios = new BLUtilitarios(strConnection,"System.Data.SqlClient");

            try
            {
                return objBLUtilitarios.ListarBancos();
            }
            catch (Exception)
            {
                return new Collection<string>();
            }
            finally
            {
                Mouse.OverrideCursor = null;
            }

        }
        #endregion           

        #region HabilitaSelecionarArquivo
        private void HabilitaSelecionarArquivo(bool pblnHabilita)
        {
            if (txtCaminhoArquivo != null)
            {
                txtCaminhoArquivo.IsEnabled = pblnHabilita;
                txtCaminhoArquivo.Text = string.Empty;
            }

            if (btnEscolherBase != null)
                btnEscolherBase.IsEnabled = pblnHabilita;

            if (cbxDataBase != null)
            {
                cbxDataBase.IsEnabled = !pblnHabilita;
                cbxDataBase.Text = string.Empty;
            }
        }
        #endregion 

        

        #endregion             

       

        





    }
}
